//package wu-framework-lazy-orm-solon-starter.processor;
//
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.BeansException;
//import org.springframework.beans.factory.FactoryBean;
//import org.springframework.beans.factory.InitializingBean;
//import org.springframework.beans.factory.config.BeanPostProcessor;
//import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
//import org.wu.framework.core.Ordered;
//import org.wu.framework.core.annotation.AnnotatedElementUtils;
//import org.wu.framework.core.io.ClassPathScriptResource;
//import org.wu.framework.core.io.FileSystemScriptResource;
//import org.wu.framework.core.io.ScriptResource;
//import org.wu.framework.core.io.UrlScriptResource;
//import org.wu.framework.core.utils.ObjectUtils;
//import wu-framework-lazy-orm-solon-starter.util.ScanClassUtil;
//import org.wu.framework.lazy.orm.database.LazyOperation;
//import org.wu.framework.lazy.orm.core.config.DdlConfigure;
//import org.wu.framework.lazy.orm.core.config.LazyOperationAttribute;
//import org.wu.framework.lazy.orm.core.config.enums.DDLAuto;
//import org.wu.framework.lazy.orm.core.config.prop.LazyDataSourceAttribute;
//import org.wu.framework.lazy.orm.core.config.prop.SchemaScript;
//import org.wu.framework.lazy.orm.core.source.SqlSourceClass;
//import org.wu.framework.lazy.orm.core.source.adapter.SourceFactory;
//import org.wu.framework.lazy.orm.core.stereotype.LazyScan;
//import org.wu.framework.lazy.orm.database.sql.expand.database.persistence.factory.LazyDataSourcePropertiesFactory;
//
//import java.util.ArrayList;
//import java.util.Arrays;
//import java.util.List;
//
///**
// * LazyBeanPostProcessor
// * DDL 操作
// *
// * @author : Jia wei Wu
// * @version 1.0
// * @date : 2022/1/2 6:23 下午
// */
//@Slf4j
//public class LazyBeanPostProcessor implements Ordered {
//    private final LazyOperation operation;
//    private final LazyOperationAttribute operationConfig;
//    private final LazyDataSourceAttribute lazyDataSourceAttribute;
//
//    // 默认已经自动化schema
//    private boolean alreadyAutoSchema = false;
//
//    public LazyBeanPostProcessor(LazyOperation operation, LazyOperationAttribute operationConfig, LazyDataSourceAttribute lazyDataSourceAttribute) {
//        this.operation = operation;
//        this.operationConfig = operationConfig;
//        this.lazyDataSourceAttribute = lazyDataSourceAttribute;
//    }
//
//    /**
//     * Apply this {@code BeanPostProcessor} to the given new bean instance <i>before</i> any bean
//     * initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
//     * or a custom init-method). The bean will already be populated with property values.
//     * The returned bean instance may be a wrapper around the original.
//     * <p>The default implementation returns the given {@code bean} as-is.
//     *
//     * @param bean     the new bean instance
//     * @param beanName the name of the bean
//     * @return the bean instance to use, either the original or a wrapped one;
//     * if {@code null}, no subsequent BeanPostProcessors will be invoked
//     * @throws BeansException in case of errors
//     * @see InitializingBean#afterPropertiesSet
//     */
//    public Object postProcessBeforeInitialization(Object bean, String beanName)  {
//        DdlConfigure ddlConfigure = operationConfig.getDdlConfigure();
//        String ddlPerfectCommitment = ddlConfigure.getDdlPerfectCommitment();
//        DDLAuto ddlAuto = ddlConfigure.getDdlAuto();
//
//
//        SourceFactory.defaultLazyDataSourceType = lazyDataSourceAttribute.getLazyDataSourceType();
//        List<Class<?>> tableClass = new ArrayList<>();
//        if (AnnotatedElementUtils.hasAnnotation(bean.getClass(), LazyScan.class)) {
//            LazyScan lazyScan = AnnotatedElementUtils.findMergedAnnotation(bean.getClass(), LazyScan.class);
//            Class<?>[] scanClass = lazyScan.scanClass();
//            tableClass.addAll(Arrays.asList(scanClass));
//            final String[] scanBasePackages = lazyScan.scanBasePackages();
//            final Class<?>[] scanBasePackageClasses = lazyScan.scanBasePackageClasses();
//            // 含有包路径
//            if (!ObjectUtils.isEmpty(scanBasePackages)) {
//                for (String scanBasePackage : scanBasePackages) {
//                    final List<Class<?>> classes = ScanClassUtil.scanClass(scanBasePackage, null);
//                    tableClass.addAll(classes);
//                }
//            }
//            // 包含 包路径的class
//            if (!ObjectUtils.isEmpty(scanBasePackageClasses)) {
//                for (Class<?> scanBasePackageClass : scanBasePackageClasses) {
//                    final List<Class<?>> classes = ScanClassUtil.scanClass(scanBasePackageClass.getPackage().getName(), null);
//                    tableClass.addAll(classes);
//                }
//            }
//        }
//        boolean enableAutoSchema = operationConfig.isEnableAutoSchema();
//        if (enableAutoSchema && !alreadyAutoSchema) {
//            try {
//                //创建数据库
//                LazyDataSourcePropertiesFactory.initSchema(lazyDataSourceAttribute);
//                alreadyAutoSchema = true;
//            } catch (Exception e) {
//                e.printStackTrace();
//                log.info("自动治愈数据库失败");
//            }
//        }
//
//        for (Class<?> clazz : tableClass) {
//            try {
//                if (DDLAuto.CREATE.equals(ddlAuto)) {
//                    operation.createTable(clazz);
//                } else if (DDLAuto.UPDATE.equals(ddlAuto)) {
//                    operation.updateTable(clazz);
//                } else if (DDLAuto.PERFECT.equals(ddlAuto)) {
//                    if (ObjectUtils.isEmpty(ddlPerfectCommitment)) {
//                        log.warn("您没有填写DDL 完善表承诺内容，当前模式将不更改任何数据库，请配置如 spring.lazy.ddl-configure.ddl-perfect-commitment: 我承诺完善表结构，支持表字段删除操作");
//                    } else {
//                        operation.perfect(clazz);
//                    }
//                } else if (DDLAuto.NONE.equals(ddlAuto)) {
//                    SqlSourceClass sqlSourceClass = SqlSourceClass.getInstance(clazz);
//                    sqlSourceClass.getLazyTableEndpoint();
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//                log.error("无法通过class: " + clazz.getName() + " 映射表");
//            }
//        }
//
//
//        return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
//    }
//
//
//    @Override
//    public int getOrder() {
//        return 0;
//    }
//
//    public void afterPropertiesSet() throws Exception {
//        List<ScriptResource> resources = new ArrayList<>();
//        SchemaScript schemaScript = operationConfig.getDdlConfigure().getSchemaScript();
//
//        // resource 下对应的初始化sql
//        for (String classPath : schemaScript.getClassPathResources()) {
//            ClassPathScriptResource classPathResource = new ClassPathScriptResource(classPath);
//            resources.add(classPathResource);
//        }
//        // 对应当前服务器下sql文件地址
//        for (String fileSystem : schemaScript.getFileSystemResources()) {
//            FileSystemScriptResource classPathResource = new FileSystemScriptResource(fileSystem);
//            resources.add(classPathResource);
//        }
//        // 对应网络服务器下sql文件地址
//        for (String url : schemaScript.getUrlResources()) {
//            UrlScriptResource classPathResource = new UrlScriptResource(url);
//            resources.add(classPathResource);
//        }
//        if (!resources.isEmpty()) {
//            try {
//                operation.scriptRunner(resources.toArray(new ScriptResource[0]));
//            } catch (Exception e) {
//                log.error("执行sql 文件失败{}", e.getMessage());
//                e.printStackTrace();
//            }
//        }
//
//    }
//}
